package com.fastbee.oss.service.impl;

import java.util.List;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.oss.config.DynamicFileStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.oss.mapper.OssConfigMapper;
import com.fastbee.oss.domain.OssConfig;
import com.fastbee.oss.service.IOssConfigService;

/**
 * 文件存储配置Service业务层处理
 *
 * @author zhuangpeng.li
 * @date 2023-03-30
 */
@Service
public class OssConfigServiceImpl implements IOssConfigService
{
    @Autowired
    private OssConfigMapper ossConfigMapper;
    @Autowired
    private DynamicFileStorage dynamicFileStorage;

    /**
     * 查询文件存储配置
     *
     * @param id 文件存储配置主键
     * @return 文件存储配置
     */
    @Override
    public OssConfig selectOssConfigById(Integer id)
    {
        return ossConfigMapper.selectOssConfigById(id);
    }

    /**
     * 查询文件存储配置列表
     *
     * @param ossConfig 文件存储配置
     * @return 文件存储配置
     */
    @Override
    public List<OssConfig> selectOssConfigList(OssConfig ossConfig)
    {
        return ossConfigMapper.selectOssConfigList(ossConfig);
    }

    /**
     * 新增文件存储配置
     *
     * @param ossConfig 文件存储配置
     * @return 结果
     */
    @Override
    public int insertOssConfig(OssConfig ossConfig)
    {
        ossConfig.setCreateTime(DateUtils.getNowDate());
        dynamicFileStorage.add(ossConfig);
        return ossConfigMapper.insertOssConfig(ossConfig);
    }

    /**
     * 修改文件存储配置
     *
     * @param ossConfig 文件存储配置
     * @return 结果
     */
    @Override
    public int updateOssConfig(OssConfig ossConfig)
    {
        ossConfig.setUpdateTime(DateUtils.getNowDate());
        dynamicFileStorage.update(ossConfig);
        return ossConfigMapper.updateOssConfig(ossConfig);
    }

    /**
     * 批量删除文件存储配置
     *
     * @param ids 需要删除的文件存储配置主键
     * @return 结果
     */
    @Override
    public int deleteOssConfigByIds(Integer[] ids)
    {
        for (Integer id : ids) {
            OssConfig ossConfig = ossConfigMapper.selectOssConfigById(id);
            dynamicFileStorage.remove(ossConfig);
        }
        return ossConfigMapper.deleteOssConfigByIds(ids);
    }

    /**
     * 删除文件存储配置信息
     *
     * @param id 文件存储配置主键
     * @return 结果
     */
    @Override
    public int deleteOssConfigById(Integer id)
    {
        OssConfig ossConfig = ossConfigMapper.selectOssConfigById(id);
        dynamicFileStorage.remove(ossConfig);
        return ossConfigMapper.deleteOssConfigById(id);
    }
}
